package com.controller;

import com.dao.*;
import com.po.Note;
import com.po.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet({"/function/selectNote", "/function/craeteNote", "/exitNote", "/function/selectNotes"
        , "/leaveEnvelope", "/givePraise", "/leave"})
public class NoteFunction extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        String servletPath = request.getServletPath();
        if (session != null && session.getAttribute("id") != null) {
            if ("/function/selectNote".equals(servletPath)) {
                doSelectNote(request, response);
            } else if ("/exitNote".equals(servletPath)) {
                doExitNote(request, response);
            } else if ("/function/craeteNote".equals(servletPath)) {
                doCreateNote(request, response);
            } else if ("/function/selectNotes".equals(servletPath)) {
                doSelectNotes(request, response);
            } else if ("/leaveEnvelope".equals(servletPath)) {
                doLeave(request, response);
            } else if ("/givePraise".equals(servletPath)) {
                doGive(request, response);
            } else if ("/leave".equals(servletPath)) {
                doL(request, response);
            }
        } else {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }
    }


    private void doL(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        session.removeAttribute("envelopeId");
        session.removeAttribute("envelopeName");
        session.removeAttribute("myLikes");
        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        String envelope_name = request.getParameter("envelope_name");
        request.getRequestDispatcher("/function/selectNotes?envelope_id=" + envelope_id + "&envelope_name=" + envelope_name).forward(request, response);
    }

    private void doGive(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        int id = (int) session.getAttribute("id");
        request.setCharacterEncoding("UTF-8");
        String state = request.getParameter("state");
        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        String envelope_name = request.getParameter("envelope_name");

        int note_id = Integer.parseInt(request.getParameter("note_id"));
        String note_name = request.getParameter("note_name");

        if ("like".equals(state)) {
            NoteDao.LikeNote(note_id);
            InboxDao.inboxTip(id, UserDao.getID(envelope_id), note_name,"praise");
            GivePraiseDao.IntoLike(id, note_id);
            request.getRequestDispatcher("/leave?envelope_id=" + envelope_id + "&envelope_name=" + envelope_name).forward(request, response);
        } else {
            NoteDao.DislikeNote(note_id);
            GivePraiseDao.CancelLike(id, note_id);
            request.getRequestDispatcher("/leave?envelope_id=" + envelope_id + "&envelope_name=" + envelope_name).forward(request, response);
        }
    }

    private void doLeave(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        session.removeAttribute("envelopeId");
        session.removeAttribute("envelopeName");
        response.sendRedirect(request.getContextPath() + "/function/openEnvelope");
    }

    private void doSelectNotes(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        int id = (int) session.getAttribute("id");
        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        String envelope_name = request.getParameter("envelope_name");
        if (session.getAttribute("envelopeId") == null) {
            session.setAttribute("envelopeId", envelope_id);
            session.setAttribute("envelopeName", envelope_name);
            session.setAttribute("myLike", UserDao.likeNote(id));
        }
        if ("administrator".equals(session.getAttribute("duty"))) {
            List<Integer> frozenId = AdministratorDao.getFrozenId(AdministratorDao.getFrozenUser());
            request.setAttribute("frozenId", frozenId);
        }
        List<Note> notes = NoteDao.getNotes(envelope_id);

        if (notes.isEmpty()) {
            out.print("该信封还没有纸条");
            out.println("<a href=\"" + request.getContextPath() + "/function/openEnvelope\">总信封页面</a>");
        } else {
            request.setAttribute("notes", notes);
            request.getRequestDispatcher("/note/openNote.jsp").forward(request, response);
        }
    }


    private void doCreateNote(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession(false);
        PrintWriter out = response.getWriter();

        int id = (int) session.getAttribute("envelope_id");
        String envelope_name = (String) session.getAttribute("envelope_name");

        String note_name = request.getParameter("note_name");
        String note_content = request.getParameter("note_content");
        if (note_name.length() >= 1 && note_content.length() >= 1) {
            int count = NoteDao.createNote(note_content, id, note_name);

            if (count == 1) {
                out.println("创建成功");
                out.println("<a href=\"" + request.getContextPath() + "/function/myEnvelope\">返回信封页面</a>");
            } else {
                out.println("创建失败");
                out.println("<a href=\"" + request.getContextPath() + "/function/myEnvelope\">返回信封页面</a>");
            }
        } else {
            out.print("输入有空，请返回重新输入");
        }
    }

    private void doExitNote(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        session.removeAttribute("envelope_id");
        session.removeAttribute("envelope_name");
        request.getRequestDispatcher("/function/myEnvelope").forward(request, response);
    }

    private void doSelectNote(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        String envelope_name = request.getParameter("envelope_name");

        HttpSession session = request.getSession(false);
        if (session.getAttribute("envelope_id") == null) {
            session.setAttribute("envelope_id", envelope_id);
            session.setAttribute("envelope_name", envelope_name);
        }

        List<Note> notes = NoteDao.getNotes(envelope_id);

        if (notes.isEmpty()) {
            out.print("该信封还没有纸条");
            out.println("<a href=\"" + request.getContextPath() + "/note/createNote.jsp\">创建纸条</a>");
        } else {
            request.setAttribute("notes", notes);
            request.getRequestDispatcher("/note/MyNote.jsp").forward(request, response);
        }
    }
}

